using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using ICP_MS.Plugin.AnalyteDb.Properties;

namespace ICP_MS.Plugin.AnalyteDb;

public class AnalyteDbPlugin : Extension
{
	[Import("Shell", typeof(ContainerControl))]
	private ContainerControl Shell { get; set; }

	public override int Priority => 200;

	public override void Activate()
	{
		IHeaderControl header = base.App.HeaderControl;
		header.Add(new RootItem("kHome", Lang.Txt("iMass.Menu.File.Text"))
		{
			SortOrder = -100
		});
		if (!header.CheckKey("kNew"))
		{
			header.Add(new SimpleActionItem("kHome", Lang.Txt("iMass.Menu.File.New.Text"), null)
			{
				Key = "kNew"
			});
		}
		if (!header.CheckKey("kOpen"))
		{
			header.Add(new SimpleActionItem("kHome", Lang.Txt("iMass.Menu.File.Open.Text"), null)
			{
				Key = "kOpen"
			});
		}
		header.Add(new SimpleActionItem("kHome", Lang.Txt("iMass.Menu.File.New.AnalyteDB.Text"), AnalyteDb_New)
		{
			MenuContainerKey = "kNew"
		});
		header.Add(new SimpleActionItem("kHome", Lang.Txt("iMass.Menu.File.Open.AnalyteDB.Text"), AnalyteDb_Open)
		{
			MenuContainerKey = "kOpen"
		});
		Shell = base.App.Shell;
		base.Activate();
	}

	public override void Deactivate()
	{
		base.Deactivate();
	}

	private void AnalyteDb_New(object sender, EventArgs e)
	{
		OpenAnalyteDb(GetTempDB());
	}

	private void AnalyteDb_Open(object sender, EventArgs e)
	{
		OpenFileDialog dlg = new OpenFileDialog();
		dlg.Filter = "数据库(*.sjk)|*.sjk";
		dlg.Title = Lang.Txt("AnalyteDb.Text6");
		if (dlg.ShowDialog() == DialogResult.OK)
		{
			OpenAnalyteDb(dlg.FileName, exist: true);
		}
	}

	private void OpenAnalyteDb(string path, bool exist = false)
	{
		FormAnalyteDB form = new FormAnalyteDB(path, exist);
		if (!form.InitDB())
		{
			MessageBox.Show(Lang.Txt("AnalyteDb.Text7"));
			form.Dispose();
			return;
		}
		string tempName = Path.GetFileNameWithoutExtension(path);
		SimpleActionItem headItem = new SimpleActionItem(tempName, delegate
		{
			if (form != null)
			{
				form.Activate();
				form.BringToFront();
			}
		})
		{
			Tag = form,
			LargeImage = Resources.AnalyteDB,
			SmallImage = Resources.AnalyteDB,
			Direction = Direction.Vertical,
			ToggleGroupKey = "kLeftBar",
			GroupCaption = "Advance",
			Size = 72
		};
		base.App.HeaderControl.Add(headItem);
		form.FormClosed += delegate
		{
			base.App.HeaderControl.Remove(headItem.Key);
			form.Dispose();
			form = null;
			GC.Collect();
		};
		form.Activated += delegate
		{
			headItem.Toggle();
		};
		form.Text = tempName;
		form.MdiParent = Shell as Form;
		form.Show();
	}

	private string GetTempDB()
	{
		string tempPreName = Path.GetTempPath() + Lang.Txt("FormScanLA.Text9");
		string temp = null;
		int count = 1;
		while (true)
		{
			temp = tempPreName + count + ".sjk";
			if (!File.Exists(temp))
			{
				break;
			}
			count++;
		}
		using (FileStream fs = new FileStream(temp, FileMode.Create, FileAccess.Write))
		{
			using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ICP_MS.Plugin.AnalyteDb.default.sjk");
			stream.CopyTo(fs, (int)stream.Length);
			fs.Flush();
		}
		return temp;
	}
}
